package com.xry.match.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xry.common.bean.Match;
import com.xry.common.bean.Match;
import com.xry.common.service.MatchService;
import com.xry.common.service.MatchService;
import com.xry.match.mapper.MatchMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86152
 * @description 针对表【match】的数据库操作Service实现
 * @createDate 2025-09-04 19:58:40
 */
@Service
@DubboService
public class MatchServiceImpl extends ServiceImpl<MatchMapper, Match>
        implements MatchService {
    @Override
    public Match getMatchById(Integer id) {
        LambdaQueryWrapper<Match> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Match::getId, id).eq(Match::getDeleted, 0);
        return super.getOne(queryWrapper);
    }


    @Override
    public boolean updateMatchById(Match match) {
        return super.updateById(match);
    }

    @Override
    public boolean deleteMatchById(Integer id) {
        Match match = super.getById(id);
        if (match != null){
            match.setDeleted(1);
            return super.updateById( match);
        }
        return false;
    }

    @Override
    public boolean addMatch(Match match) {
        return super.save(match);
    }

    @Override
    public List<Match> getAll() {
        return super.list();
    }

    @Override
    public IPage<Match> getPage(Match match, IPage<Match> page) {
        LambdaQueryWrapper<Match> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (match != null) {
            // 比赛名称模糊查询
            if (StringUtils.hasText(match.getName())) {
                queryWrapper.like(Match::getName, match.getName());
            }

            // 参赛方A模糊查询
            if (StringUtils.hasText(match.getTeama())) {
                queryWrapper.like(Match::getTeama, match.getTeama());
            }

            // 参赛方B模糊查询
            if (StringUtils.hasText(match.getTeamb())) {
                queryWrapper.like(Match::getTeamb, match.getTeamb());
            }

            // 场馆ID精确查询
            if (match.getStadiumId() != null) {
                queryWrapper.eq(Match::getStadiumId, match.getStadiumId());
            }

            // 比赛状态精确查询
            if (match.getStatus() != null) {
                queryWrapper.eq(Match::getStatus, match.getStatus());
            }

            // 比赛时间范围查询
            if (match.getMatchTime() != null) {
                queryWrapper.ge(Match::getMatchTime, match.getMatchTime());
            }
        }

        // 只查询未删除的记录
        queryWrapper.eq(Match::getDeleted, 0);

        // 按比赛时间倒序排列
        queryWrapper.orderByDesc(Match::getMatchTime);

        return super.page(page, queryWrapper);
    }
}


